//
//  LZEditGroupOrderViewController.m
//  laziz_Merchant
//
//  Created by ZhaoDongBo on 2016/12/19.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZEditGroupOrderViewController.h"
#import "LZEditGroupOrderView.h"
#import "Masonry.h"
#import "LZGroupItemModel.h"
#import "BlocksKit+UIKit.h"
#import "LZRemindView.h"
#import "LZAlertView.h"

@interface LZEditGroupOrderViewController ()

@property (nonatomic, strong) LZEditGroupOrderView * view1;
@property (nonatomic, strong) LZEditGroupOrderView * view2;

@property (nonatomic, strong) UIButton * confirmButton;

@end

@implementation LZEditGroupOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.groupItemModel == nil) {
        self.umLogPageViewName = @"增加优惠菜单页面";
        self.title = LZLocalizedString(@"add_object_of_the_group", nil);
    } else {
        self.umLogPageViewName = @"修改优惠菜单页面";
        self.title = LZLocalizedString(@"edit_object_of_the_group", nil);
    }
    
    self.view.backgroundColor = UIColorFromRGB(0xffffff, 1);
    
    [self configViews];
    [self addAutoLayout];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)configViews {
    
    [self.view addSubview:self.view1];
    [self.view addSubview:self.view2];
    [self.view addSubview:self.confirmButton];
    
    if (self.groupItemModel) {
        self.view1.textField.text = [NSString stringWithFormat:@"%@", self.groupItemModel.foodName];
        self.view2.textField.text = [NSString stringWithFormat:@"%0.3f", self.groupItemModel.foodPrice];
    }
}

- (void)addAutoLayout {
    
    WS(weakSelf);
    
    [self.view1 makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.leading.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
        make.height.mas_equalTo(55);
    }];
    
    [self.view2 makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.view1.mas_bottom);
        make.leading.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
        make.height.mas_equalTo(55);
    }];
    
    [self.confirmButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.view2.mas_bottom).with.offset(40);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(44);
    }];
}

#pragma mark - Setter/Getter

- (LZEditGroupOrderView *)view1 {
    
    if (_view1 == nil) {
        _view1 = [[LZEditGroupOrderView alloc] init];
        _view1.titleLabel.text          = LZLocalizedString(@"name", nil);
        _view1.textField.placeholder    = LZLocalizedString(@"enter_name", nil);
    }
    
    return _view1;
}

- (LZEditGroupOrderView *)view2 {
    
    if (_view2 == nil) {
        _view2 = [[LZEditGroupOrderView alloc] init];
        _view2.titleLabel.text          = LZLocalizedString(@"original_price", nil);
        _view2.textField.placeholder    = LZLocalizedString(@"enter_original_price", nil);
        _view2.textField.keyboardType   = UIKeyboardTypeNumbersAndPunctuation;
        
        WS(weakSelf);
        _view2.textFieldEditDidEndBlock = ^(NSString * text, NSInteger tag) {
            if ([text floatValue] < 0 || [text floatValue] > 100) {
                
                weakSelf.view2.textField.text = @"";
                LZAlertView *alert = [[LZAlertView alloc] initWithContent:LZLocalizedString(@"msg_original_price", nil)
                                                                 andImage:nil
                                                        cancelButtonTitle:LZLocalizedString(@"cancel", nil)
                                                        otherButtonTitles:nil,nil];
                [alert show];
            }
        };
    }
    
    return _view2;
}

- (UIButton *)confirmButton {
    
    WS(weakSelf);
    if (_confirmButton == nil) {
        _confirmButton = [[UIButton alloc] init];
        _confirmButton.backgroundColor        = UIColorFromRGB(0x33a928, 1);
        _confirmButton.titleLabel.font        = [UIFont systemFontOfSize:17.0];
        _confirmButton.layer.masksToBounds    = YES;
        _confirmButton.layer.cornerRadius     = 44/2.0f;
        [_confirmButton setTitle:LZLocalizedString(@"confirm", nil) forState:UIControlStateNormal];
        [_confirmButton setTitleColor:UIColorFromRGB(0xffffff, 1) forState:UIControlStateNormal];
        [_confirmButton bk_addEventHandler:^(id sender) {
            
            if (weakSelf.view1.textField.text.length == 0) {
                [LZRemindView initWithImage:[UIImage imageNamed:@"错误提示"] andContent:LZLocalizedString(@"enter_name", @"")];
                return;
            }
            
            if (weakSelf.view2.textField.text.length == 0) {
                [LZRemindView initWithImage:[UIImage imageNamed:@"错误提示"] andContent:LZLocalizedString(@"enter_original_price", @"")];
                return;
            }
            
            if ([weakSelf.view2.textField.text floatValue] < 0 || [weakSelf.view2.textField.text floatValue] > 100) {
                return;
            }
            
            if (weakSelf.delegate) {
                
                if (weakSelf.groupItemModel == nil) {
                    [weakSelf.delegate addGroupItem:weakSelf.view1.textField.text andPrict:[weakSelf.view2.textField.text floatValue]];
                } else {
                    weakSelf.groupItemModel.foodName = weakSelf.view1.textField.text;
                    weakSelf.groupItemModel.foodPrice = [weakSelf.view2.textField.text floatValue];
                    [weakSelf.delegate editGroupItem:weakSelf.groupItemModel];
                }
                
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }

        } forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _confirmButton;
}

@end
